local Class = require('lua.lang.Class')

--- @class StringAndNumberEncryptor : Class
--- @field lexer Lexer
--- @field outputStream OutputStream
local StringAndNumberEncryptor = Class {}

--- @param lexer Lexer
--- @param outputStream OutputStream
--- @return StringAndNumberEncryptor
function StringAndNumberEncryptor:new(lexer, outputStream)
	--- @type StringAndNumberEncryptor
	local o = {}
	o.lexer = lexer
	o.outputStream = outputStream
	return setmetatable(o, StringAndNumberEncryptor)
end

--- @param handler fun(t: LexToken): string
--- @return void
function StringAndNumberEncryptor:encrypt(handler)
	local os = self.outputStream
	local lexer = self.lexer
	local t = lexer:getToken()
	while not t:isEof() do
		local bytes = handler(t):toByteArray()
		for i = 1, #bytes do
			os:write(bytes[i])
		end
		t = lexer:getToken()
	end
end

return StringAndNumberEncryptor